﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class System_DeptManage : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            initData();
          
        }
    }
    private void initData() 
    {
        string sql = "select *,(select count(1) from CS_Staff where deptId=department.deptid) as ProNum from department";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ProNum = DataBinder.Eval(e.Row.DataItem, "ProNum").ToString();

            if (Convert.ToInt32(ProNum) > 0)
            {
                (e.Row.Cells[7].FindControl("ckFlag") as CheckBox).Enabled = false;
            }
        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        Response.Redirect("Dept.aspx?moduid=" + Request["moduid"] + "&action=add");

    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        ZSql inf = new ZSql();
        ZSql zs = new ZSql();
        if (this.gvData.Rows.Count > 0)
        {
            for (int i = 0; i < gvData.Rows.Count; i++)
            {
                CheckBox ck = gvData.Rows[i].Cells[7].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    inf.Open("select * from UserInfo where deptid='" + this.gvData.Rows[i].Cells[0].Text+"'");
                    if (inf.m_table.Rows.Count > 0)
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('所选部门有下属成员，不能删除')", true);
                    }
                    else
                    {
                        zs.Open("delete department  where deptid='" + this.gvData.Rows[i].Cells[0].Text + "'");
                    }
                  
                   
                }
                else
                {
                    n++;
                }

                if (n == this.gvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
            //Response.Write("<script>window.location='DeptManage.aspx?moduid=" + Request["moduid"] + "';</script>");
        }
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string sql = "select * from department";
        if (!string.IsNullOrEmpty(txtName.Text))
        {
            sql += " where deptbh like '" +Common.FormatParameter(txtName.Text) + "%'";
        }

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("Dept.aspx?moduid="+Request["moduid"]+"&id="+id+"&action=update");
    }

}
